﻿
using Newtonsoft.Json;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace Bot.Domain
{
    public class QueryIntentCallTime
    {
        public int OneSecondTimes { get; set; }
        public int ThreeSecondTimes { get; set; }
        public int FiveSecondTimes { get; set; }
        public int TenSecondTimes { get; set; }
        public int TwentySecondTimes { get; set; }
        public int ThirtySecondTimes { get; set; }
        public int SixtySecondTimes { get; set; }
        public int SixtyPlusSecondTimes { get; set; }

        public DateTime LastWriteLogTime { get; set; }

        public void Add(int seconds)
        {
            if(seconds <= 1)
            {
                OneSecondTimes++;
            }
            else if (seconds > 1 && seconds<=3)
            {
                ThreeSecondTimes++;
            }
            else if (seconds > 3 && seconds <= 5)
            {
                FiveSecondTimes++;
            }
            else if (seconds > 5 && seconds <= 10)
            {
                TenSecondTimes++;
            }
            else if (seconds > 10 && seconds <= 20)
            {
                TwentySecondTimes++;
            }
            else if (seconds > 20 && seconds <= 30)
            {
                ThirtySecondTimes++;
            }
            else if (seconds > 30 && seconds <= 60)
            {
                SixtySecondTimes++;
            }
            else if (seconds > 60)
            {
                SixtyPlusSecondTimes++;
            }
        }
        private void Restore()
        {
            this.OneSecondTimes = 0;
            this.ThreeSecondTimes = 0;
            this.FiveSecondTimes = 0;
            this.TenSecondTimes = 0;
            this.TwentySecondTimes = 0;
            this.ThirtySecondTimes = 0;
            this.SixtySecondTimes = 0;
            this.SixtyPlusSecondTimes = 0;
            this.LastWriteLogTime = DateTime.UtcNow;
        }

        public void WriteLog()
        {
            double hour = 1;

            //跨天
            if (LastWriteLogTime.AddDays(1).Day == DateTime.UtcNow.Day)
            {
                LogHelper.Error(JsonConvert.SerializeObject(this));
                LastWriteLogTime = DateTime.UtcNow;
                Restore();
            }

            TimeSpan timeSpan = DateTime.UtcNow - LastWriteLogTime;
            if (timeSpan.TotalHours >= hour)
            {
                LogHelper.Error(JsonConvert.SerializeObject(this));
                LastWriteLogTime = DateTime.UtcNow;
            }

        }
    }
}
